<template>
  <!-- 导航部分 -->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>设备管理</el-breadcrumb-item>
        <el-breadcrumb-item>点检保养计划</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

  <!--搜索栏-->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-form inline>
        <el-form-item label="计划编码">
          <el-input v-model="planQuery.planCode"/>
        </el-form-item>
        <el-form-item label="计划名称">
          <el-input v-model="planQuery.planName"/>
        </el-form-item>
        <el-form-item label="计划类型">
          <el-select style="width: 150px" v-model="planQuery.planType">
          <el-option label="全部"/>
          <el-option value="设备点检计划" label="设备点检计划"/>
          <el-option value="设备保养计划" label="设备保养计划"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select style="width: 100px;" v-model="planQuery.planStatus">
          <el-option label="全部"/>
          <el-option value="1" label="草稿"/>
          <el-option value="2" label="已确认"/>
          <el-option value="3" label="审批中"/>
          <el-option value="4" label="已审批"/>
          <el-option value="5" label="已完成"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="loadPlan" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

  <!-- 工具栏 -->
  <el-row style="margin: 20px">
    <el-col :span="24">
      <el-button type="primary" plain @click="toAdd"><el-icon><Plus /></el-icon>新增</el-button>
      <el-button type="success" plain @click="toUpdate"><el-icon><EditPen /></el-icon>编辑</el-button>
      <el-button type="warning" plain @click="toDeletes"><el-icon><EditPen /></el-icon>删除</el-button>
    </el-col>
  </el-row>

<!--  &lt;!&ndash; 工具栏 &ndash;&gt;-->
<!--  <el-row style="margin: 10px">-->
<!--    <el-col :span="24">-->
<!--      <el-button type="primary" @click="toAdd">新增</el-button>-->
<!--      <el-button type="primary" @click="toUpdate">编辑</el-button>-->
<!--      <el-button type="primary" @click="toDeletes">删除</el-button>-->
<!--    </el-col>-->
<!--  </el-row>-->

  <!-- 数据列表 -->
  <el-row style="margin: 0px 10px;padding: 0px 15px">
    <el-col :span="24">
      <el-table :data="plans" border stripe ref="planTable">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="planCode" label="计划编码">
          <template #default="scope">
            <el-link @click="showDetail(scope.row.planId)" type="success">
              {{scope.row.planId}}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="planName" label="计划名称"/>
        <el-table-column prop="planType" label="计划类型"/>
        <el-table-column prop="startTime" label="开始时间"/>
        <el-table-column prop="endTime" label="结束时间"/>
        <el-table-column prop="planRate" label="频率"/>
        <el-table-column prop="planStatus" label="状态">
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.planStatus == 1">草稿</el-tag>
            <el-tag type="warning" v-if="scope.row.planStatus == 2">已确认</el-tag>
            <el-tag type="warning" v-if="scope.row.planStatus == 3">审批中</el-tag>
            <el-tag type="warning" v-if="scope.row.planStatus == 4">已审批</el-tag>
            <el-tag type="warning" v-if="scope.row.planStatus == 5">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-link style="margin: 0px 5px" @click="toEdit(scope.row.planId)" type="primary">
              <el-icon>
                <EditPen/>
              </el-icon>
              编辑
            </el-link>
            <el-link style="margin: 0px 5px" @click="toDelete(scope.row.planId)" type="primary">
              <el-icon>
                <Delete/>
              </el-icon>
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>

  <el-row style="margin-top: 10px">
    <!-- 监听页码的变化事件 -->
    <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
  </el-row>

  <!-- 新增的弹窗 -->
  <el-dialog v-model="addPlanVisible"
             title="新增计划"
             width="50%"
  >
    <el-form :model="newPlan">
      <el-form-item label="计划编码">
        <el-input v-model="newPlan.planCode"/>
      </el-form-item>
      <el-form-item label="计划名称">
        <el-input v-model="newPlan.planName"/>
      </el-form-item>
<!--      <el-form-item label="计划类型">-->
<!--        <el-input v-model="newPlan.planType"/>-->
<!--      </el-form-item>-->
      <el-form-item label="计划类型">
        <el-radio-group v-model="newPlan.planType">
          <el-radio value="设备检点" size="large">设备检点计划</el-radio>
          <el-radio value="设备保养" size="large">设备保养计划</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="频率">
        <el-input-number v-model="newPlan.planRate" :min="1" :max="9999" />
      </el-form-item>
<!--      <el-form-item label="开始日期">-->
<!--        <el-input v-model="newPlan.startTime"/>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="结束日期">-->
<!--        <el-input v-model="newPlan.endTime"/>-->
<!--      </el-form-item>-->
      <el-row>
        <el-col :span="12">
          <el-form-item label="开始时间">
            <el-date-picker
                v-model="newPlan.startTime"
                type="date"
                value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间">
            <el-date-picker
                v-model="newPlan.endTime"
                type="date"
                value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="状态">
        <el-select style="width: 100px" v-model="newPlan.planStatus">
          <el-option :value="1" label="草稿"/>
          <el-option :value="2" label="已确认"/>
          <el-option :value="3" label="审批中"/>
          <el-option :value="4" label="已审批"/>
          <el-option :value="5" label="已完成"/>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="newPlan.planNotes"/>
      </el-form-item>

    </el-form>
    <el-button @click="addPlanVisible =false" newPlan={}>取消</el-button>
    <el-button type="primary" @click="addPlan">
      确定
    </el-button>
  </el-dialog>

  <!--编辑子组件-->
  <PlanEdit :planId="updatePlanId" @editComplate="loadPlan"/>

  <!--详情子组件-->
  <planDetail :planId="datailPlanId"/>


</template>

<script setup>

import {onMounted, ref} from "vue";
import maintainPlanRequest from "@/api/maintainPlan.js";
import {ElMessage, ElMessageBox} from "element-plus";
import ItemEdit from "@/views/machineType/machineMaintainItemsEdit.vue";
import PlanEdit from "@/views/machineType/machineMaintainPlanEdit.vue";
import maintainItemsRequest from "@/api/maintainItems.js";
import LedgerDetail from "@/views/machineType/machineLedgerDetail.vue";
import planDetail from "@/views/machineType/machineMaintainPlanDetail.vue";
import {EditPen} from "@element-plus/icons-vue";
let planQuery =ref({});
let plans = ref([]);
let planTable = ref([]);
let curPage = ref(1);
let total = ref(0);
let addPlanVisible = ref(false);
let newPlan = ref({});
let updatePlanId = ref(-1);
let datailPlanId = ref(-1);
const num = ref(1);

//显示详情
function showDetail(planId){
  datailPlanId.value = planId;
}
//工具栏新增按钮
function addPlan(){
  maintainPlanRequest.addPlan(newPlan.value,res=>{
  if (res.code == 200){
    ElMessage({
      type:"success",
      message:"添加成功"
    })
  }else {
    ElMessage({
      type:"warning",
      message:"添加失败"
    })
  }
  newPlan.value = {};
  addPlanVisible.value =false;
  loadPlan();
})
}
//工具栏新增
function toAdd(){
  addPlanVisible.value=true;
}
//工具栏编辑
function toUpdate(){
  let selectionRows = planTable.value.getSelectionRows();
  if (selectionRows.length > 1){
    ElMessage({
      type:"warning",
      message:"只能选择一条数据"
    })
  }else if(selectionRows.length == 0){
    ElMessage({
      type:"warning",
      message:"请选择数据"
    })
  }else{
    toEdit(selectionRows[0].planId);
  }
}
//工具栏删除
function toDeletes(){
  let selectionRows = planTable.value.getSelectionRows();
  if (selectionRows.length == 0 ){
    ElMessage({
      type:"warning",
      message:"请选择数据"
    })
    return;
  }
  ElMessageBox.confirm(
      '您确定要删除吗？',
      'warning',
      {
        confirmButtonClass:'是的',
        cancelButtonClass:'取消',
        type:'warning',
      }
  ).then(()=>{
    selectionRows.forEach(row=>{
      maintainPlanRequest.deletePlan(row.planId,res=>{
        console.log(row.planId);
        if (res.code == 1){
          ElMessage({
            type:"success",
            message:"删除成功",
          })
        }else {
          ElMessage({
            type:"warning",
            message:"删除失败",
          })
        }
      })
    })
    loadPlan();
  })
}
//行编辑
function toEdit(planId){
  updatePlanId.value = planId;
}
//行删除
function toDelete(planId){
  ElMessageBox.confirm(
      '您确定要删除吗',
      'Warning',
      {
        confirmButtonClass:'是的',
        cancelButtonClass:'取消',
        type:'warning',
      }
  ).then(()=>{
    maintainPlanRequest.deletePlan(planId,res=>{
      if (res.code == 1){
        ElMessage({
          type:"success",
          message:"操作完成"

        })
        loadPlan();
      }else {
        ElMessage({
          type:"warning",
          message:"删除失败"
        })
      }
    })
    loadPlan();
  })
}

function loadPlan(){
  maintainPlanRequest.loadPlan(planQuery.value,curPage.value,10,res=>{
    total.value = res.data.total;
    plans.value = res.data.list;
  })
}
function changePage(value){
  curPage.value = value;
  loadPlan();
}
onMounted(()=>{
  loadPlan();
})
</script>

<style scoped>

</style>
